﻿using ArtMath.Component.Point;
using ArtMath.PointCollection;
using System;

namespace ArtMath.Component.Polygon
{
    /// <summary>
    /// 矩形
    /// </summary>
    [Serializable]
    public class Rectangle : ParallelogramBase
    {
        public Rectangle(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.jx, addToList)
        {
        }
        //protected override bool CheckCharacteristic(PointPoint line1, PointPoint line2, PointPoint line3, PointPoint line4)
        //{
        //    return Utility.AlmostEqual((line1.Point2.X - line1.Point1.X) * (line4.Point1.X - line4.Point2.X) + (line1.Point2.Y - line1.Point1.Y) * (line4.Point1.Y - line4.Point2.Y), 0);
        //}
        public override void ComponentBuilding(Base component)
        {
            if (component == Line2)
            {
                Line2.AllowFix = false;
                Line2.SpareCoordinate_ = Line2.SpareCoordinate.GetProjectivePoint(Line2.Point1.Pxy, Pad.kToPk(-1 / Line1.Slope));
                Line2.AllowFix = true;
            }
            base.ComponentBuilding(component);
        }
        protected override PointCollectionBase[] collections()
        {
            return new PointCollectionBase[] { new StraightLine(Line2.Point1.ToPointD(), -1 / Line1.Slope) };
        }
        public override PointCollectionBase[] GetInPointMovingRange(PointBase movePoint, Base sender = null)
        {
            switch (CountMovablePoints())
            {
                case 2:
                    if (movePoint == Line1.Point1)
                    {
                        if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line4.Point1.NewXY, -1 / Line3.NewSlope) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, Line2.Point1.NewXY.DistanceTo(Line4.Point1.NewXY) / 2) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line2.Point1.NewXY, -1 / Line2.NewSlope) };
                        }
                    }
                    else if (movePoint == Line2.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line3.Point1.NewXY, -1 / Line3.NewSlope) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line1.Point1.NewXY, -1 / Line4.NewSlope) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, Line1.Point1.NewXY.DistanceTo(Line3.Point1.NewXY) / 2) };
                        }
                    }
                    else if (movePoint == Line3.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, Line2.Point1.NewXY.DistanceTo(Line4.Point1.NewXY) / 2) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line4.Point1.NewXY, -1 / Line4.NewSlope) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line2.Point1.NewXY, -1 / Line1.NewSlope) };
                        }
                    }
                    else if (movePoint == Line4.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line3.Point1.NewXY, -1 / Line2.NewSlope) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, Line1.Point1.NewXY.DistanceTo(Line3.Point1.NewXY) / 2) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine(Line1.Point1.NewXY, -1 / Line1.NewSlope) };
                        }
                    }
                    break;
            }
            return base.GetInPointMovingRange(movePoint, sender);
        }
    }
}
